//
//  ZbarViewController.m
//  PurchasingMeeting
//
//  Created by yanseng.lin on 9/3/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "ZbarViewController.h"

@interface ZbarViewController ()

@end

@implementation ZbarViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setNavTitle:@"扫描二维码"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img = [[UIHelper getInstance] getBundleImage:@"anniu.png"];
    [b setBackgroundImage:img forState:UIControlStateNormal];
    [b setTitle:@"返回" forState:UIControlStateNormal];
    b.frame = CGRectMake(10, 10, 99, 37);
    [b addTarget:self action:@selector(ret) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:b];
    self.navigationItem.leftBarButtonItem = left;
    
    UIImage *bgImage = [[UIHelper getInstance] getBundleImage:@"suit_bg.jpg"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:bgImage];
    
    img = [[UIHelper getInstance] getBundleImage:@"anniu.png"];
    UIButton *scan = [UIButton buttonWithType:UIButtonTypeCustom];
    [scan setBackgroundImage:img forState:UIControlStateNormal];
    [scan setTitle:@"扫描" forState:UIControlStateNormal];
    scan.frame = CGRectMake(0, 0, img.size.width, img.size.height);
    scan.center = CGPointMake(self.view.width/2, 30);
    [scan addTarget:self action:@selector(scan:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:scan];
    
    imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
    imageview.center = CGPointMake(self.view.width/2, self.view.height/2-20);
    [self.view addSubview:imageview];
    
    label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 30)];
    label.backgroundColor = [UIColor clearColor];
    label.centerX = self.view.width/2;
    label.top = imageview.bottom+10;
    label.textAlignment = UITextAlignmentCenter;
    label.text = @"生成码";
    [self.view addSubview:label];
}

-(void)ret
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (void) imagePickerController: (UIImagePickerController*) reader
// didFinishPickingMediaWithInfo: (NSDictionary*) info
//{
//    id<NSFastEnumeration> results =
//    [info objectForKey: ZBarReaderControllerResults];
//    ZBarSymbol *symbol = nil;
//    for(symbol in results)
//        break;
//    
//    imageview.image =
//    [info objectForKey: UIImagePickerControllerOriginalImage];
//    
//    [reader dismissModalViewControllerAnimated: YES];
//    
//    //判断是否包含 头'http:'
//    NSString *regex = @"http+:[^\\s]*";
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
//    
//    //判断是否包含 头'ssid:'
//    NSString *ssid = @"ssid+:[^\\s]*";;
//    NSPredicate *ssidPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",ssid];
//    
//    label.text =  symbol.data ;
//    
//    if ([predicate evaluateWithObject:label.text]) {
//        
//        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil
//                                                        message:@"It will use the browser to this URL。"
//                                                       delegate:nil
//                                              cancelButtonTitle:@"Close"
//                                              otherButtonTitles:@"Ok", nil];
//        alert.delegate = self;
//        alert.tag=1;
//        [alert show];
//        
//        
//        
//    }
//    else if([ssidPre evaluateWithObject:label.text]){
//        
//        NSArray *arr = [label.text componentsSeparatedByString:@";"];
//        
//        NSArray * arrInfoHead = [[arr objectAtIndex:0] componentsSeparatedByString:@":"];
//        
//        NSArray * arrInfoFoot = [[arr objectAtIndex:1] componentsSeparatedByString:@":"];
//        
//        
//        label.text=
//        [NSString stringWithFormat:@"ssid: %@ \n password:%@",
//         [arrInfoHead objectAtIndex:1],[arrInfoFoot objectAtIndex:1]];
//        
//        
//        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:label.text
//                                                        message:@"The password is copied to the clipboard , it will be redirected to the network settings interface"
//                                                       delegate:nil
//                                              cancelButtonTitle:@"Close"
//                                              otherButtonTitles:@"Ok", nil];
//        
//        
//        alert.delegate = self;
//        alert.tag=2;
//        [alert show];
//        
//        UIPasteboard *pasteboard=[UIPasteboard generalPasteboard];
//        //        然后，可以使用如下代码来把一个字符串放置到剪贴板上：
//        pasteboard.string = [arrInfoFoot objectAtIndex:1];
//        
//        
//    }
//}

-(void)scan:(UIButton *)sender
{
//    ZBarReaderViewController *reader = [ZBarReaderViewController new];
//    reader.readerDelegate = self;
//    reader.supportedOrientationsMask = ZBarOrientationMaskAll;
//    
//    ZBarImageScanner *scanner = reader.scanner;
//    
//    [scanner setSymbology: ZBAR_I25
//                   config: ZBAR_CFG_ENABLE
//                       to: 0];
//    
//    [self presentModalViewController: reader
//                            animated: YES];
}
@end
